﻿@page "/settings"

@using BXJG.Utils.RCL.Settings
@using BXJG.Utils.Application.Share.Settings

@attribute [ReuseTabsPage(Title = "设置中心")]
@attribute [Authorize(BXJG.Utils.Application.Share.Settings.Changliang.PermissionNameGet)]
@inherits SettingManagerComponent


<Flex Gap="FlexGap.Middle">
    <Card Style="height:85vh;min-width:17rem; ">
        <Tree TItem="SettingDefinitionGroupDto" @ref="tree"
              DataSource="DataSource" DefaultSelectedKey="@defaultSelectedKey"
              ChildrenExpression="x=>x.DataItem.Children"
              IsLeafExpression="x=>x.DataItem.Children==null||x.DataItem.Children.Count==0"
              KeyExpression="x=>x.DataItem.Name" OnSelect="x=>LoadCuree(x.Node.DataItem)">

            <TitleTemplate>
                @context.DataItem.DisplayName <span style="color:orange">(@context.DataItem.ChildrenCount)</span>
            </TitleTemplate>

        </Tree>
    </Card>

    <div style="display:block;width:100%; max-height:84vh; overflow:auto;">
        <Form Model="@this" Style="width:70%; margin:0 auto;"
              LabelColSpan="6" OnFinish="Save" Loading=@isLoading
              WrapperColSpan="18">
            @foreach (var item in this.CurrItems)
            {
                string csharpType = "string";
                if (item.CustomData.TryGetValue("csharpType", out var csharpType1))
                {
                    csharpType = csharpType1.ToLower();
                }
                bool isRequired = false;
                if (item.CustomData.TryGetValue("isRequired", out var isRequired1))
                {
                    isRequired = isRequired1.Equals("True", StringComparison.OrdinalIgnoreCase);
                }
                //默认值
                // item.CustomData.TryGetValue("defaultValue", out var defaultValue);


                <FormItem Required=@isRequired>
                    <LabelTemplate>
                        <div class="frm-item-label-box">
                            @if (item.Description.IsNotNullOrWhiteSpaceBXJG())
                            {
                                <Tooltip>
                                    <TitleTemplate>
                                        @item.Description
                                    </TitleTemplate>
                                    <ChildContent>

                                        <Icon Style="" Type="@IconType.Outline.QuestionCircle" Width="1.1rem" Height="1.1rem" />

                                    </ChildContent>
                                </Tooltip>
                            }
                            @item.DisplayName：
                        </div>
                    </LabelTemplate>
                    <ChildContent>
                        @if (csharpType == "int")
                        {
                            var mrz = item.DefaultValue.IsNullOrWhiteSpaceBXJG() ? default : int.Parse(item.DefaultValue);

                            if (item.CustomData.TryGetValue("selectValues", out var selectValues))
                            {
                                //若可选数量少，可以用Radio组件
                                var kx = System.Text.Json.JsonSerializer.Deserialize<Dictionary<int, string>>(selectValues);

                                <Select ItemLabel="x => x.Value" Value="@(int.Parse(item.Value))" ValueChanged="x => item.Value = x.ToString()" ItemValue="x => x.Key" DefaultValue="@mrz" DataSource="kx" TItem="KeyValuePair<int,string>" TItemValue="int" />
                            }
                            else
                            {
                                <AntDesign.InputNumber TValue="int" Value="@(int.Parse(item.Value))" ValueChanged="x => item.Value = x.ToString()" />
                            }
                        }
                        else if (csharpType == "bool")
                        {
                            //var mrz = item.DefaultValue.IsNullOrWhiteSpaceBXJG() ? default : item.DefaultValue.Equals("true", StringComparison.OrdinalIgnoreCase);
                            <Switch Checked="@(item.Value.Equals("true", StringComparison.OrdinalIgnoreCase))" CheckedChanged="x => item.Value = x.ToString()" />
                        }
                        else
                        {
                            var mrz = item.DefaultValue;//.IsNullOrWhiteSpaceBXJG() ? default : int.Parse(item.DefaultValue);

                            if (item.CustomData.TryGetValue("selectValues", out var selectValues))
                            {
                                //若可选数量少，可以用Radio组件
                                var kx = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, string>>(selectValues);

                                <Select ItemLabel="x => x.Value" Value="@item.Value" ValueChanged="x => item.Value = x" ItemValue="x => x.Key" DefaultValue="@mrz" DataSource="kx" TItem="KeyValuePair<string,string>" TItemValue="string" />
                            }
                            else
                            {
                                <Input DefaultValue="@mrz" @bind-Value="@item.Value" />
                            }


                        }
                    </ChildContent>
                </FormItem>
            }


            <FormItem WrapperColOffset="6" WrapperColSpan="18">
                <Button Type="ButtonType.Primary" HtmlType="submit" Loading=@isBusy Icon="@IconType.Outline.Save">
                    保持
                </Button>
            </FormItem>
        </Form>
    </div>
</Flex>